package com.lkd.controller;


import cn.hutool.core.bean.BeanUtil;
import com.lkd.entity.User;
import com.lkd.service.UserService;
import com.lkd.vo.PageBean;
import com.lkd.vo.Result;
import com.lkd.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-20
 */
@RestController
@RequestMapping("/api/user-service/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("search")
    public Result search(String userName,
                         @RequestParam(defaultValue = "1")Integer pageIndex,
                         @RequestParam(defaultValue = "10")Integer pageSize){
        PageBean<UserVo> page = userService.search(userName,pageIndex,pageSize);
        return Result.success(page);
    }

    //新增人员
    @PostMapping
    public Result add(@RequestBody UserVo userVo){
        User user = BeanUtil.copyProperties(userVo, User.class);
        user.setStatus(userVo.getStatus()==true?1:2);
        userService.save(user);
        return Result.success();
    }

    //修改人员
    @PutMapping
    public Result update(@RequestBody UserVo userVo){
        User user = BeanUtil.copyProperties(userVo, User.class);
        user.setStatus(userVo.getStatus()==true?1:2);
        userService.updateById(user);
        return Result.success();
    }

    //删除人员
    @DeleteMapping("/{userId}")
    public Result delete(@PathVariable Integer userId){
        userService.removeById(userId);
        return Result.success();
    }
}
